Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macro for JsonDeserializer extractor to avoid boilerplate code #2849

Conversation

manuelgdlvh
Copy link

@manuelgdlvh manuelgdlvh commented Jul 26, 2024

Macro for the JsonDeserializer extractor to avoid boilerplate code in most situations where the handler returns a Response or its derivatives, and returns early in case of failure.

Motivation

In most situations, the controller layer is responsible for managing the result of the service layer and returning a specific response (not an expected result). Given this scenario, using the JsonDeserializer extractor, we should handle both when the payload is correct and when it is not.
This macro returns the expected payload, and if not, it returns early a Response structure, avoiding duplicate code.

Solution

A macro that returns the payload if everything went well, otherwise it returns the error as a Response structure that implements IntoResponse.

Macro for JsonDeserializer extractor to avoid boilerplate code in mostly situations where handler return Response or derived and returns early if fails
@jplatte jplatte self-assigned this Jul 26, 2024
@jplatte
Copy link
Member

jplatte commented Jul 26, 2024

I don't think this makes a lot of sense. Have you considered changing your handler to return axum::response::Result<JsonDeserializer<T>>, call .deserialize()? and wrapping your other return values in Ok()?

@manuelgdlvh
Copy link
Author

manuelgdlvh commented Jul 26, 2024

Hi @jplatte ,
i made this small change to avoid exactly that,
put Ok() and Result<, _> in all handlers considering that handlers must to manage all resulting of below layers and prepare each type of response.
Something like bail! macro (more or less) in anyhow crate does to keep less boilerplate!

@jplatte
Copy link
Member

jplatte commented Jul 26, 2024

Well, using ? is usually much easier than macros for early returns. It's not really expected that many handlers return exactly a Response so I don't think this macro is useful for many users of axum.

If you want, you can use the Discussions section to discuss the merit of this kind of macro or your style of writing handlers more generally, but I'm going to close this PR because I'm fairly certain it doesn't make sense for axum to include this macro.

Thanks for the PR anyways though! If you have other ideas for how to improve axum, feel free to open issues or discussions about it, or send a PR if it's not much effort.

@jplatte jplatte closed this Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants